<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>

<body>

<!--解决弹出层中无法触发日期选择器的方案-->
<script id="importBox2" type="text/html">
    <div style="padding: 20px 80px;"><input type="text" id="openDate" readOnly="readonly" lay-verify="date" placeholder="点击选择日期时间" autocomplete="off" class="layui-input"></div>
</script>

<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">外链ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="linkId" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">用户昵称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nickName" autocomplete="off" class="layui-input">
                            </div>
                        </div>


<!--                        <div class="layui-inline">-->
<!--                            <label class="layui-form-label" readonly="readonly">推广类型</label>-->
<!--                            <div class="layui-input-inline">-->
<!--                                <select name="linkType">-->
<!--                                    <option value="">全部</option>-->
<!--                                    <option value="0">文章链接</option>-->
<!--                                    <option value="1">小程序跳转</option>-->
<!--                                </select>-->
<!--                            </div>-->
<!--                        </div>-->



                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 批量删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">指定失效日期</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate;


        table.render({
            elem: '#currentTableId',
            url: '../web/listLinkAll',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [{
                    type: "checkbox",
                    width: 50
                },{
                    field: 'nickName',
                    width: 90,
                    title: '创建用户',
                    align: "center"
                },{
                    field: 'avatarUrl',
                    width: 90,
                    title: '用户头像',
                    align: "center",
                    templet:function (res) {
                        return "<div><img src="+res.avatarUrl+" width='30px' height='30px'></div>"
                    }
                },{
                    field: 'linkId',
                    width: 110, title: '外链ID',
                    align: "center"
                },{
                    field: 'linkName',
                    title: '链接备注',
                    align: "center"
                },{
                    field: 'linkUrl',
                    title: '文章链接',
                    align: "center"
                },{
                    field: 'clickCount',
                    width: 110,
                    title: '点击次数',
                    sort: true,
                    align: "center"
                },{
                    field: 'triggerCount',
                    width: 110,
                    title: '触发次数',
                    sort: true,
                    align: "center"
                },{
                    field: 'createTime',
                    title: '创建时间',
                    sort: true,
                    align: 'center',
                    templet : "<div>{{layui.util.toDateString(d.createTime, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"
                },{
                    field: 'expireTime',
                    title: '过期时间',
                    sort: true,
                    align: "center",
                    templet : "<div>{{layui.util.toDateString(d.expireTime, 'yyyy年MM月dd日 HH:mm:ss')}}</div>"
                },{
                    title: '操作',
                    toolbar: '#currentTableBar',
                    align: "center"
                }]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line row'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            // layer.alert(result, {
            //     title: '最终的搜索信息'
            // });
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result,
                    linkId:data.field.linkId,
                    nickName:data.field.nickName,
                }
            }, 'data');

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'delete') {  // 监听批量删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.msg('加载中', {
                    icon: 16,
                    shade: 0.50,
                    time:5000
                });
                $.ajax({
                    url:"../web/deleteBatchById",
                    data:JSON.stringify(data),
                    dataType:'JSON',
                    contentType : "application/json",
                    type:'POST',
                    success :function (res) {
                        if (res.statusCode===200) {
                            layer.msg(res.message, {icon: 1,time:1000},function () {
                                table.reload('currentTableId', {})
                                return;
                            });
                        }else {
                            layer.msg(res.message, {icon: 2});
                        }
                    }
                })

            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                //边缘弹出
                layer.open({
                    type: 1
                    ,offset: 'auto' //具体配置参考：offset参数项
                    ,content: $("#importBox2").html()
                    ,btn: '确认修改'
                    ,btnAlign: 'c' //按钮居中
                    ,title:"操作ID："+data.linkId
                    ,shade: 0.2 //不显示遮罩
                    ,yes: function(){
                        var thisDate = $('#openDate').val();
                        if(thisDate){
                            data.expireTime = thisDate;
                            console.log(data);
                            $.get('../web/updateExpireTime', data, function (res) {
                                console.log(res)
                                if (res.statusCode===200) {
                                    layer.msg(res.message, {icon: 1,time:1000},function () {
                                        table.reload('currentTableId', {})
                                        layer.closeAll();
                                        return;
                                    });
                                }else {
                                    layer.msg(res.message, {icon: 2});
                                    layer.close(index);
                                }
                            }, 'json')
                            return;
                        }
                        layer.msg("请选择日期", {icon: 2,time:500});
                    }, success: function (layero, index) {
                        var ssss = layui.util.toDateString(data.expireTime, 'yyyy-MM-dd HH:mm:ss');
                        $('#openDate').val(ssss+"");
                        laydate.render({
                            elem: '#openDate',//指定元素
                            type: 'datetime',
                        });
                    }
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么？', function (index) {
                    layer.msg('加载中', {
                        icon: 16,
                        shade: 0.50,
                        time:5000
                    });
                    $.get('../web/deleteById', data, function (res) {
                        console.log(res)
                        if (res.statusCode===200) {
                            layer.msg(res.message, {icon: 1,time:1000},function () {
                                layer.close(index);
                                obj.del();
                                return;
                            });
                        }else {
                            layer.msg(res.message, {icon: 2});
                            layer.close(index);
                        }
                    }, 'json')
                });
            }
        });

    });
</script>



</body>
</html>